import API from './API'
import axios from 'axios'
import { getBaseUrl, getQueryString } from './Utils'

let Service = {
    async getGoods (params, { isServer, req }) {
        return await axios.get(getBaseUrl(isServer, req) + API.goods + '?' + getQueryString(params))
    },
    async getEsGoods (params, { isServer, req, store }) {
        return await axios.post(getBaseUrl(isServer, req) + API.search.replace('{domainKey}', store.state.domain), params)
    },
    async getCategory (params, { isServer, req }) {
        return await axios.get(getBaseUrl(isServer, req) + API.category + '?' + getQueryString(params))
    },
    async getGoodDetail (skuId, { isServer, req }) {
        let { data } = await axios.get(getBaseUrl(isServer, req) + API.goodDetail.replace('{skuId}', skuId))
        let resourceTemp = []
        for (let item of data.result.resources) {
            if (item.image) {
                resourceTemp.push(item)
            }
        }
        data.result.resources = resourceTemp
        for (let item of data.result.skuInfoList) {
            item.count = 0
            item.image = ''
            item.brand = data.result.spuBaseInfo.bwcBrandName
            if (data.result.resources && data.result.resources.length > 0) {
                item.image = data.result.resources[0].image
            }
        }
        return data
    },
    async getGoodSkuList (spuId) {
        return axios.get(API.goodSkuList.replace('{spuId}', spuId)).then(data => {
            data = data.result
            for (let item of data.skuInfoList) {
                item.count = 0
                item.image = ''
                item.brand = data.spuBaseInfo.bwcBrandName
                if (data.resources && data.resources.length > 0) {
                    item.image = data.resources[0].image
                }
            }
            return data
        })
    },
    async createOrder (params) {
        return await axios.post(getBaseUrl(false) + API.order, params)
    },
    async getHaoGYS (domain) {
        let { data } = await axios.get(API.haogys.replace('{domainKey}', domain))
        return data
    },
    async getCorpInfo (domain, { isServer, req }) {
        return await axios.get(getBaseUrl(isServer, req) + API.corpInfo.replace('{domainKey}', domain))
    },
    async getInquiryList (key, { isServer, req }) {
        return await axios.get(getBaseUrl(isServer, req) + API.order + '/' + key)
    },
}

export default Service